﻿Imports Microsoft.Win32

Partial Public Class AlertSettingsWindow

    Public Shared ReadOnly SendMailCommand As RoutedCommand = New RoutedCommand("SendMail", GetType(GeneralSettingsWindow))
    Public Shared ReadOnly PlaySoundCommand As RoutedCommand = New RoutedCommand("PlaySound", GetType(GeneralSettingsWindow))
    Public Shared ReadOnly SetDefaultCommand As RoutedCommand = New RoutedCommand("SetDefault", GetType(GeneralSettingsWindow))

    Private ReadOnly Property Context() As UserSettings
        Get
            Return DirectCast(Me.DataContext, UserSettings)
        End Get
    End Property


    Private _targets As IList(Of AlertTarget)
    Private _selectAlertDataContext As New SelectAlertDataContext

    Public Sub New(ByVal targets As IList(Of AlertTarget), ByVal setting As UserSettings)
        Me.InitializeComponent()
        Me.DataContext = setting
        Me.BindingGroup.BeginEdit()

        MailPasswordTextBox.Password = Me.Context.Mail.Password.Decrypt

        _targets = targets

        If _targets Is Nothing OrElse _targets.Count = 0 Then
            ' デフォルト通知設定
            DescriptionTextBox.Text = "既定の通知設定をします。"
            DefaultAlertGrid.DataContext = setting.DefaultAlert
            Exit Sub
        Else
            DescriptionTextBox.Text = targets.Count.ToString & "個の放送局の通知設定をします。"
            DefaultAlertGrid.DataContext = _selectAlertDataContext
        End If

        Dim first = targets.First
        With _selectAlertDataContext

            If targets.All(Function(t) t.AlertByWindow = first.AlertByWindow) Then
                .AlertByWindow = first.AlertByWindow
            Else
                .AlertByWindow = Nothing
            End If

            If targets.All(Function(t) t.AlertByBalloon = first.AlertByBalloon) Then
                .AlertByBalloon = first.AlertByBalloon
            Else
                .AlertByBalloon = Nothing
            End If

            If targets.All(Function(t) t.AlertByBrowser = first.AlertByBrowser) Then
                .AlertByBrowser = first.AlertByBrowser
            Else
                .AlertByBrowser = Nothing
            End If

            If targets.All(Function(t) t.AlertByMail = first.AlertByMail) Then
                .AlertByMail = first.AlertByMail
            Else
                .AlertByMail = Nothing
            End If

            If targets.All(Function(t) t.AlertBySound = first.AlertBySound) Then
                .AlertBySound = first.AlertBySound
            Else
                .AlertBySound = Nothing
            End If

        End With

    End Sub

#Region "Browser"
    Private Sub BrowserPathClearButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        'Me.Context.Browser.Path = ""
        BrowserPathTextBox.Clear()
    End Sub

    Private Sub BrowserPathButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim dialog = New OpenFileDialog With { _
            .Filter = "アプリケーション ファイル(*.exe)|*.exe"}

        If dialog.ShowDialog Then
            Me.Context.Browser.Path = dialog.FileName
        End If
    End Sub
#End Region

#Region "SendMail Command"
    Private Sub SendMailCommandBinding_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)

        Dim group = Me.BindingGroup
        Dim settings As MailSettings = Nothing

        For Each g In group.Items
            If TypeOf g Is MailSettings Then
                settings = DirectCast(g, MailSettings)
                Exit For
            End If
        Next

        If settings Is Nothing Then
            Exit Sub
        End If

        Dim toAddress As Object = Nothing
        If Not group.TryGetValue(settings, "ToAddress", toAddress) Then
            Exit Sub
        End If

        Dim fromAddress As Object = Nothing
        If Not group.TryGetValue(settings, "FromAddress", fromAddress) Then
            Exit Sub
        End If

        Dim smtpServer As Object = Nothing
        If Not group.TryGetValue(settings, "SmtpServer", smtpServer) Then
            Exit Sub
        End If

        Dim port As Object = Nothing
        If Not group.TryGetValue(settings, "Port", port) Then
            Exit Sub
        End If

        Dim encoding As Object = Nothing
        If Not group.TryGetValue(settings, "Encoding", encoding) Then
            Exit Sub
        End If

        Dim userName As Object = Nothing
        If Not group.TryGetValue(settings, "UserName", userName) Then
            Exit Sub
        End If

        Dim enableSsl As Object = Nothing
        If Not group.TryGetValue(settings, "EnableSsl", enableSsl) Then
            Exit Sub
        End If

        MailManager.Send("テスト送信", My.Resources.ApplicationName, _
                         CStr(toAddress), CStr(fromAddress), CStr(smtpServer), CInt(port), CStr(encoding), CBool(enableSsl), CStr(userName), MailPasswordTextBox.Password.Encrypt)

    End Sub

    Private Sub SendMailCommandBinding_CanExecute(ByVal sender As System.Object, ByVal e As System.Windows.Input.CanExecuteRoutedEventArgs)
        Dim group = Me.BindingGroup
        Dim settings As MailSettings = Nothing

        For Each g In group.Items
            If TypeOf g Is MailSettings Then
                settings = DirectCast(g, MailSettings)
                Exit For
            End If
        Next

        If settings Is Nothing Then
            Exit Sub
        End If

        Dim toAddress As Object = Nothing
        If Not group.TryGetValue(settings, "ToAddress", toAddress) OrElse _
            DirectCast(toAddress, String) = "" Then
            Exit Sub
        End If

        Dim fromAddress As Object = Nothing
        If Not group.TryGetValue(settings, "FromAddress", fromAddress) OrElse _
            DirectCast(fromAddress, String) = "" Then
            Exit Sub
        End If

        Dim smtpServer As Object = Nothing
        If Not group.TryGetValue(settings, "SmtpServer", smtpServer) OrElse _
            DirectCast(smtpServer, String) = "" Then
            Exit Sub
        End If

        Dim port As Object = Nothing
        If Not group.TryGetValue(settings, "Port", port) OrElse _
            CStr(port).Trim = "" Then
            Exit Sub
        End If

        Dim encoding As Object = Nothing
        If Not group.TryGetValue(settings, "Encoding", encoding) OrElse _
            DirectCast(encoding, String) = "" Then
            Exit Sub
        End If

        e.CanExecute = True
    End Sub
#End Region

#Region "Sound"

#Region "PlaySound Command"
    Private Sub PlaySoundCommandBinding_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        SoundManager.PlaySound(SoundPathTextBox.Text)
    End Sub

    Private Sub PlaySoundCommandBinding_CanExecute(ByVal sender As System.Object, ByVal e As System.Windows.Input.CanExecuteRoutedEventArgs)
        If SoundPathTextBox.Text.Trim <> "" Then
            e.CanExecute = True
        End If
    End Sub
#End Region


    Private Sub SoundPathButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim dialog = New OpenFileDialog With { _
             .Filter = "サウンド ファイル(*.wav, *.mid, *.mp3, *.wma)|*.wav;*.mid;*.midi;*.mp3;*.wma"}

        If dialog.ShowDialog Then
            Me.Context.Sound.Path = dialog.FileName
        End If
    End Sub

    Private Sub SoundPathClearButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        'Me.Context.Sound.Path = ""
        SoundPathTextBox.Clear()
    End Sub

#End Region

#Region "OK Cancel"

    Private Sub OkButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

        Me.Context.Mail.Password = MailPasswordTextBox.Password.Encrypt
        BindingGroup.CommitEdit()

        If _targets Is Nothing OrElse _targets.Count = 0 Then
            DefaultAlertGrid.BindingGroup.CommitEdit()
            Me.Close()
            Exit Sub
        End If

        Dim group = DefaultAlertGrid.BindingGroup
        Dim settings As Object
        settings = DirectCast(group.Items(0), SelectAlertDataContext)


        Dim byWindow As Object = Nothing
        If group.TryGetValue(settings, "AlertByWindow", byWindow) Then
            If DirectCast(byWindow, Boolean?) IsNot Nothing Then
                For Each t In _targets
                    t.AlertByWindow = CBool(byWindow)
                Next
            End If
        End If

        Dim byBalloon As Object = Nothing
        If group.TryGetValue(settings, "AlertByBalloon", byBalloon) Then
            If DirectCast(byBalloon, Boolean?) IsNot Nothing Then
                For Each t In _targets
                    t.AlertByBalloon = CBool(byBalloon)
                Next
            End If
        End If

        Dim byBrowser As Object = Nothing
        If group.TryGetValue(settings, "AlertByBrowser", byBrowser) Then
            If DirectCast(byBrowser, Boolean?) IsNot Nothing Then
                For Each t In _targets
                    t.AlertByBrowser = CBool(byBrowser)
                Next
            End If
        End If

        Dim byMail As Object = Nothing
        If group.TryGetValue(settings, "AlertByMail", byMail) Then
            If DirectCast(byMail, Boolean?) IsNot Nothing Then
                For Each t In _targets
                    t.AlertByMail = CBool(byMail)
                Next
            End If
        End If


        Dim bySound As Object = Nothing
        If group.TryGetValue(settings, "AlertBySound", bySound) Then
            If DirectCast(bySound, Boolean?) IsNot Nothing Then
                For Each t In _targets
                    t.AlertBySound = CBool(bySound)
                Next
            End If
        End If


        Me.Close()
    End Sub

    Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Me.BindingGroup.CancelEdit()
        Me.Close()
    End Sub

#End Region


    Private Sub SetDefaultCommandBinding_Executed(ByVal sender As System.Object, ByVal e As System.Windows.Input.ExecutedRoutedEventArgs)
        DefaultAlertGrid.BindingGroup.CommitEdit()
    End Sub

    Private Sub SetDefaultCommandBinding_CanExecute(ByVal sender As System.Object, ByVal e As System.Windows.Input.CanExecuteRoutedEventArgs)

        'Dim group = DefaultAlertGrid.BindingGroup
        'Dim settings As Object
        'If _targets Is Nothing OrElse _targets.Count = 0 Then
        '    settings = DirectCast(group.Items(0), DefaultAlertSettings)
        'Else
        '    settings = DirectCast(group.Items(0), SelectAlertDataContext)
        'End If


        'Dim byWindow As Object = Nothing
        'If group.TryGetValue(settings, "AlertByWindow", byWindow) Then
        '    If TypeOf byWindow Is Boolean OrElse _
        '       DirectCast(byWindow, Boolean?) IsNot Nothing Then
        '        For Each t In _targets
        '            t.AlertByWindow = CBool(byWindow)
        '        Next
        '    End If
        'End If

        'Dim byBalloon As Object = Nothing
        'If group.TryGetValue(settings, "AlertByBalloon", byBalloon) Then
        '    If TypeOf byBalloon Is Boolean OrElse _
        '       DirectCast(byBalloon, Boolean?) IsNot Nothing Then
        '        For Each t In _targets
        '            t.AlertByBalloon = CBool(byBalloon)
        '        Next
        '    End If
        'End If

        'Dim byBrowser As Object = Nothing
        'If group.TryGetValue(settings, "AlertByBrowser", byBrowser) Then
        '    If TypeOf byBrowser Is Boolean OrElse _
        '       DirectCast(byBrowser, Boolean?) IsNot Nothing Then
        '        For Each t In _targets
        '            t.AlertByBrowser = CBool(byBrowser)
        '        Next
        '    End If
        'End If

        'Dim byMail As Object = Nothing
        'If group.TryGetValue(settings, "AlertByMail", byMail) Then
        '    If TypeOf byMail Is Boolean OrElse _
        '       DirectCast(byMail, Boolean?) IsNot Nothing Then
        '        For Each t In _targets
        '            t.AlertByMail = CBool(byMail)
        '        Next
        '    End If
        'End If


        'Dim bySound As Object = Nothing
        'If group.TryGetValue(settings, "AlertBySound", bySound) Then
        '    If TypeOf bySound Is Boolean OrElse _
        '       DirectCast(bySound, Boolean?) IsNot Nothing Then
        '        For Each t In _targets
        '            t.AlertBySound = CBool(bySound)
        '        Next
        '    End If
        'End If
    End Sub
End Class
